<?php

namespace app\admin\controller\bill;

use app\admin\model\order\OrderGoods;
use app\common\controller\Backend;
use ba\Filesystem;
use hehe\Str;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\facade\Db;

/**
 * 商品管理
 */
class Order extends Backend {
    /**
     * Goods模型对象
     * @var object
     * @phpstan-var \app\admin\model\goods\Goods
     */
    protected object $model;

    protected string|array $defaultSortField = 'id,desc';

    protected array|string $preExcludeFields = ['id', 'create_time'];

    protected string|array $quickSearchField = ['name'];

    public function initialize(): void {
        parent::initialize();
        $this->model = new \app\admin\model\goods\Goods;
    }


    /**
     * 对账单 - 订单详情统计
     */
    public function bill() {
        $param = $this->request->param();

        $order_status_all = [
            ['label' => '取消订单', 'code' => [-1]],
            ['label' => '准备下单', 'code' => [0]],
            ['label' => '已下单', 'code' => [1]],
            ['label' => '现货', 'code' => [2]],
            ['label' => '付款预定', 'code' => [3]],
            ['label' => '口头预定', 'code' => [4]],
            ['label' => '入库', 'code' => [5]],
            ['label' => '韩国已发货', 'code' => [6]],
            ['label' => '待定', 'code' => [7]],
            ['label' => '退款', 'code' => ['8-1', '8-2', '8-3']],
            ['label' => '换货', 'code' => ['9-1', '9-2', '9-3', '9-4']],
            ['label' => '已提货', 'code' => [10]],
        ];

        if($param['type'] == 0){ // 当天总计(汇总)
            $whereNotIn = [0, '8-1', '8-2', '8-3'];
        }
        if($param['type'] == 1){ // 当天账单
            $whereNotIn = [0, -1, 1, 4, 5, 7, '9-1', '9-2', '9-3', '9-4'];
        }
        if($param['type'] == 2){ // 未发货
            $whereNotIn = [-1, 0, 1, 4, 5, 7, '8-1', '8-2', '8-3'];
        }


        $orderGoodsModel = new OrderGoods();

        $field = "og.id, og.place_order_time, b.name brand_name, g.cover goods_cover, u.username, g.name goods_name,
                    og.color, og.size, g.supply_price, g.price, og.num, og.total_price, og.purchase_price, og.status, 
         og.id order_goods_id, og.pay_reserve_day, og.verbal_reserve_day, b.id brand_id, b.outstation_id";
        $where = [
            ['b.outstation_id', '=', $this->outstation['id']], // 只查询当前分站的订单
            ['o.status', '=', 1]
        ];
        $list = $orderGoodsModel->alias('og')
            ->with([
                'suborder_note' => function ($query) { // 子订单备注
                    $query->with(['admin']);
                }
            ])->field($field) // 子订单
            ->leftJoin('order o', 'og.order_id = o.id') // 订单
            ->leftJoin('user u', 'o.user_id = u.id') // 用户
            ->leftJoin('goods g', 'og.goods_id = g.id') // 商品
            ->leftJoin('goods_brand b', 'g.goods_brand_id=b.id') //品牌
            ->where($where)->whereNotIn('og.status', $whereNotIn);


        if (!empty($state['place_order_time'])) { // 下单时间
            $start_time = strtotime(date('Y-m-d 00:00:00', strtotime($state['place_order_time'][0])));
            $end_time = strtotime(date('Y-m-d 23:59:59', strtotime($state['place_order_time'][1])));
            $list->whereBetween('o.place_order_time', [$start_time, $end_time]);
        }

        if(!empty($param['daterange'])){ // 下单时间
            $start_time = strtotime(date('Y-m-d 00:00:00', strtotime($param['daterange'][0])));
            $end_time = strtotime(date('Y-m-d 23:59:59', strtotime($param['daterange'][1])));
            $list->whereBetween('o.place_order_time', [$start_time, $end_time]);
        }

        if(!empty($param['username'])){ // 用户ID
            $list->where('u.username', '=', trim($param['username']));
        }


        $list = $list->append(['status_text', 'purchase_price_text', 'price_text', 'supply_price_text', 'total_price_text'])
            ->order('og.order_id desc')
            ->select()->toArray();

//        print_r($list);die;

        $data = [
            'supply_price' => 0
        ];
        foreach ($list as $val) {
            $val['place_order_time_text'] = date('m/d H:i', $val['place_order_time']);
            $data['supply_price'] = isset($data['supply_price']) ? $data['supply_price'] + $val['supply_price'] * $val['num'] : $val['supply_price'] * $val['num'];
            foreach ($order_status_all as $k => $v) {
                if (in_array($val['status'], $v['code'])) {
                    $data['list'][$k]['label'] = $v['label'];
                    $data['list'][$k]['info'][] = $val;
                    $data['list'][$k]['num'] = isset($data['list'][$k]['num']) ? $data['list'][$k]['num'] + $val['num'] : $val['num'];


                    $data['list'][$k]['supply_price'] = isset($data['list'][$k]['supply_price']) ? $data['list'][$k]['supply_price'] + $val['supply_price'] * $val['num'] : $val['supply_price'] * $val['num'];
                    $data['list'][$k]['supply_price_text'] = number_format($data['list'][$k]['supply_price']); // 采购总价

                    $data['list'][$k]['purchase_price'] = isset($data['list'][$k]['purchase_price']) ? $data['list'][$k]['purchase_price'] + $val['purchase_price'] * $val['num'] : $val['purchase_price'] * $val['num'];
                    $data['list'][$k]['purchase_price_text'] = number_format($data['list'][$k]['purchase_price']); // DC总价
                }
            }
        }
        $data['supply_price_text'] = number_format($data['supply_price']);

        $this->success('', $data);

    }


}